package com.litong.litongjiaoyu.controller;

import com.litong.litongjiaoyu.service.UserSessionService;
import com.litong.litongjiaoyu.utils.RedisUtil;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

/**
 * @ClassName RedisTestController
 * @Description redis测试类
 * @Author jingz
 * @Date 2020/2/1 12:50
 * @Version 1.0
 **/

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class RedisTestController {

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private UserSessionService userSessionService;

    @Test
    public void mapTest() {
        Map<String,Object> message = new HashMap<>(10);

        List<String> messageList = new ArrayList<>();

        messageList.add("你妈炸了");

        message.put("fromusername", 123);
        message.put("messageList", messageList);

        redisUtil.HashSet("message", message);
    }

    @Test
    public void hasHashTest(){
        assertTrue(redisUtil.hasHasKey("messageList"));
    }

    @Test
    public void ListTest(){
        List<String> messageList = new ArrayList<>();

        messageList.add("你妈炸了");

        assertTrue(redisUtil.lSet("messageList", messageList));
    }

    @Test
    public void delete(){
        redisUtil.delete("message", "messageList");
    }

    @Test
    public void String() {
        assertTrue(redisUtil.set("user-redis-session:123", 123123123, 1));
    }

    @Test
    public void hasString() {
        assertTrue(redisUtil.have("user-redis-session:ovrbN4jwxdTOv768bdlGGiT2Rzik"));
    }

    @Test
    public void getTime(){
       assertEquals("og7um5Cy5bg6z8GDyu9_D8_oDNvg", userSessionService.getValue("TK/jSTJQDoiWrdVg7wqMJw=="));
    }

    @Test
    public void addToHash() {

        List<Object> l = new ArrayList<>(3);
        l.add(1);
        l.add(2);

        redisUtil.addDataToHash("chat", "123", l);


        Object la = redisUtil.hashGet("chat", "123");

        try {
            List<Object> dd = (List<Object>) la;

            dd.add(3);

            redisUtil.addDataToHash("chat", "123", dd);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
